package com.atguigu.teach.mytask;

import com.atguigu.teach.juc.BlockingQueueDemo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @name:阻塞队列
 * @params:
 * @description:
 * @return:
 * @exception/throws:
 * @author: zxb
 * @date:   2021/10/27
 */
public class MyBlockQueueDemo {
    public static void main(String[] args) {
     List list=new ArrayList();
        BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<String>(3);
        System.out.println("抛出异常组方法=======================");
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        //System.out.println(blockingQueue.add("x"));
        //返回第一个
        System.out.println(blockingQueue.element());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println("特殊组异常组方法=======================");
        System.out.println(blockingQueue.offer("z"));
        System.out.println(blockingQueue.offer("x"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.peek());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());

        System.out.println("阻塞组方法=======================");
        try {
           blockingQueue.put("z");
           blockingQueue.put("x");
           blockingQueue.put("b");
           blockingQueue.take();
           blockingQueue.take();
           blockingQueue.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("超时方法=======================");
        try {
            System.out.println(blockingQueue.offer("z",3, TimeUnit.SECONDS));
            System.out.println(blockingQueue.offer("x",3, TimeUnit.SECONDS));
            System.out.println(blockingQueue.offer("b",3, TimeUnit.SECONDS));
            System.out.println(blockingQueue.offer("b",3, TimeUnit.SECONDS));
            System.out.println(blockingQueue.poll(4,TimeUnit.SECONDS));
            System.out.println(blockingQueue.poll(4,TimeUnit.SECONDS));
            System.out.println(blockingQueue.poll(4,TimeUnit.SECONDS));
            System.out.println(blockingQueue.poll(4,TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
